﻿Imports System.Data.SqlClient

Public Class adm_productupdatepic
    Inherits System.Web.UI.Page
    Private sSQL As String
    Private objrd As SqlDataReader = Nothing
    Private objfind As New GetData
    Private ps As New PreparedStatement(sSQL)

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Session("admin_ID") <> Nothing And Session("admin_ID") <> "") Then
            If (Request("uid") <> Nothing And Request("uid") <> "") Then
                Dim productId, strErr, photoType, path As String
                strErr = ""
                productId = Request("productId")
                If (Request("submitproductimage") <> Nothing And Request("submitproductimage") <> "") Then
                    productId = Me.productId.Value
                    Dim i As Integer
                    For i = 0 To Request.Files.Count - 1
                        Dim myFile As HttpPostedFile = Request.Files(i)
                        If (myFile.ContentLength <> 0) Then
                            System.IO.Directory.CreateDirectory(Server.MapPath("../../") & "pic/product/" & productId)
                            myFile.SaveAs(Server.MapPath("../../") & "pic/product/" & productId & "/" & myFile.FileName.ToString)
                            path = "/pic/product/" & productId & "/" & myFile.FileName.ToString
                            ID = System.Guid.NewGuid().ToString()
                            photoType = myFile.FileName.Substring(myFile.FileName.LastIndexOf(".") + 1)
                            ps.strSql = "insert into product_photo(path,product_id,photo_type) values(@path,@productId,@photoType)"
                            ps.parameters.Clear()
                            ps.addParameter("path", path)
                            ps.addParameter("productId", productId)
                            ps.addParameter("photoType", True)
                            objfind.es(ps, strErr)
                        End If
                    Next
                    Response.Redirect("adm_supplyManagement.aspx?uid=" & Request("uid"))
                Else
                    Me.productId.Value = productId
                    ps.strSql = "select * from product_photo where product_id = @productId"
                    ps.parameters.Clear()
                    ps.addParameter("productId", productId)
                    Dim conn As SqlConnection = objfind.getConn()

                    objrd = objfind.dr(ps, strErr, conn, objrd)
                    Dim i As Integer
                    i = 0
                    While (objrd.Read())
                        If (objrd.Item("path").ToString <> Nothing And objrd.Item("path").ToString <> "") Then
                            If (i = 0) Then
                                imageDiv0.InnerHtml = "<img src=""" & objrd.Item("path") & """ width=""100"" height=""100"" />"
                                id0.Value = objrd.Item("id").ToString
                            End If
                            If (i = 1) Then
                                imageDiv1.InnerHtml = "<img src=""" & objrd.Item("path") & """ width=""100"" height=""100"" />"
                                id1.Value = objrd.Item("id").ToString
                            End If
                            If (i = 2) Then
                                imageDiv2.InnerHtml = "<img src=""" & objrd.Item("path") & """ width=""100"" height=""100"" />"
                                id2.Value = objrd.Item("id").ToString
                            End If
                            If (i = 3) Then
                                imageDiv3.InnerHtml = "<img src=""" & objrd.Item("path") & """ width=""100"" height=""100"" />"
                                id3.Value = objrd.Item("id").ToString
                            End If
                            i += 1
                        End If
                    End While
                    objfind.closeConn(conn)

                End If
            Else
                Response.Redirect("../memberMngt_default.aspx")
            End If
        Else
            Response.Redirect("../adm_login.aspx")
        End If
    End Sub

    Protected Sub deleteImage0_Click(ByVal sender As Object, ByVal e As EventArgs) Handles deleteImage0.Click
        Dim productId, strErr, id, path As String
        strErr = ""
        id = id0.Value
        productId = Me.productId.Value
        If (id <> Nothing And id <> "") Then
            ps.strSql = "select * from product_photo where id = @id"
            ps.parameters.Clear()
            ps.addParameter("id", id)
            Dim conn As SqlConnection = objfind.getConn()

            objrd = objfind.dr(ps, strErr, conn, objrd)
            If (objrd.Read()) Then
                path = objrd.Item("path")
                'path = path.Substring(6)
                System.IO.File.Delete(Server.MapPath("../../") & path)
                imageDiv0.InnerHtml = "<input type=""file"" name=""image"" id=""image0"" runat=""server""/>"
                Me.id0.Value = ""
                ps.strSql = "delete from product_photo where id = @id"
                ps.parameters.Clear()
                ps.addParameter("id", id)
                objfind.es(ps, strErr)
                Response.Redirect("adm_productupdatepic.aspx?productId=" & productId & "&uid=" & Request("uid"))
            End If
            objfind.closeConn(conn)

        End If
    End Sub

    Protected Sub deleteImage1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles deleteImage1.Click
        Dim productId, strErr, id, path As String
        strErr = ""
        id = id1.Value
        productId = Me.productId.Value
        If (id <> Nothing And id <> "") Then
            ps.strSql = "select * from product_photo where id = @id"
            ps.parameters.Clear()
            ps.addParameter("id", id)
            Dim conn As SqlConnection = objfind.getConn()

            objrd = objfind.dr(ps, strErr, conn, objrd)
            If (objrd.Read()) Then
                path = objrd.Item("path")
                'path = path.Substring(6)
                System.IO.File.Delete(Server.MapPath("../../") & path)
                imageDiv1.InnerHtml = "<input type=""file"" name=""image"" id=""image0"" runat=""server""/>"
                Me.id1.Value = ""
                ps.strSql = "delete from product_photo where id = @id"
                ps.parameters.Clear()
                ps.addParameter("id", id)
                objfind.es(ps, strErr)
                Response.Redirect("adm_productupdatepic.aspx?productId=" & productId & "&uid=" & Request("uid"))
            End If
            objfind.closeConn(conn)

        End If
    End Sub

    Protected Sub deleteImage2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles deleteImage2.Click
        Dim productId, strErr, id, path As String
        strErr = ""
        id = id2.Value
        productId = Me.productId.Value
        If (id <> Nothing And id <> "") Then
            ps.strSql = "select * from product_photo where id = @id"
            ps.parameters.Clear()
            ps.addParameter("id", id)
            Dim conn As SqlConnection = objfind.getConn()

            objrd = objfind.dr(ps, strErr, conn, objrd)
            If (objrd.Read()) Then
                path = objrd.Item("path")
                'path = path.Substring(6)
                System.IO.File.Delete(Server.MapPath("../../") & path)
                imageDiv2.InnerHtml = "<input type=""file"" name=""image"" id=""image0"" runat=""server""/>"
                Me.id2.Value = ""
                ps.strSql = "delete from product_photo where id = @id"
                ps.parameters.Clear()
                ps.addParameter("id", id)
                objfind.es(ps, strErr)
                Response.Redirect("adm_productupdatepic.aspx?productId=" & productId & "&uid=" & Request("uid"))
            End If
            objfind.closeConn(conn)

        End If
    End Sub

    Protected Sub deleteImage3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles deleteImage3.Click
        Dim productId, strErr, id, path As String
        strErr = ""
        id = id3.Value
        productId = Me.productId.Value
        If (id <> Nothing And id <> "") Then
            ps.strSql = "select * from product_photo where id = @id"
            ps.parameters.Clear()
            ps.addParameter("id", id)
            Dim conn As SqlConnection = objfind.getConn()

            objrd = objfind.dr(ps, strErr, conn, objrd)
            If (objrd.Read()) Then
                path = objrd.Item("path")
                'path = path.Substring(6)
                System.IO.File.Delete(Server.MapPath("../../") & path)
                imageDiv3.InnerHtml = "<input type=""file"" name=""image"" id=""image0"" runat=""server""/>"
                Me.id3.Value = ""
                ps.strSql = "delete from product_photo where id = @id"
                ps.parameters.Clear()
                ps.addParameter("id", id)
                objfind.es(ps, strErr)
                Response.Redirect("adm_productupdatepic.aspx?productId=" & productId & "&uid=" & Request("uid"))
            End If
            objfind.closeConn(conn)

        End If
    End Sub

End Class